//
//  ZQLogToolManager.m
//  iOS10LogTool
//
//

#import "ZQLogToolManager.h"
#import "ZQLog.h"
#import "ZQFloatWindow.h"

#define ZQ_LogLevel_UserDefalut @"ZQLogToolManagerLevel"

@interface ZQLogToolManager ()<UIDocumentInteractionControllerDelegate>

/**
 悬浮窗
 */
@property (nonatomic, strong) ZQFloatWindow *floatWindow;

/**
 documentInteractionController，用来显示或者分享log文件内容
 */
@property (nonatomic, strong) UIDocumentInteractionController *documentInteractionController ;

@end

@implementation ZQLogToolManager

//单例
+ (instancetype)shareManager
{
    static ZQLogToolManager *manager = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        manager = [[ZQLogToolManager alloc] init];
    });
    return manager;
}

//初始化log类型
- (void)logIntial
{
    NSInteger logLevel = [[NSUserDefaults standardUserDefaults] integerForKey:ZQ_LogLevel_UserDefalut];
    
    self.logLevel = (ZQLogToolManagerLevel)logLevel;
}



#pragma mark - setter

- (void)setLogLevel:(ZQLogToolManagerLevel)logLevel
{
    _logLevel = logLevel;
    
    NSUserDefaults *userDefault = [NSUserDefaults standardUserDefaults];
    switch (logLevel) {
        case ZQLogToolManagerLevelNone://不打印log
        {
            //处理userDefault中的level值及floatWindow显示
            [userDefault setInteger:ZQLogToolManagerLevelNone forKey:ZQ_LogLevel_UserDefalut];
            [userDefault synchronize];
            if (_floatWindow)
            {
                [_floatWindow dissmissWindow];
                _floatWindow = nil;
            }
        }
            break;
        case ZQLogToolManagerLevelLog://只在控制台显示log
        {
            [userDefault setInteger:ZQLogToolManagerLevelLog forKey:ZQ_LogLevel_UserDefalut];
            [userDefault synchronize];
            
            if (_floatWindow)
            {
                [_floatWindow dissmissWindow];
                _floatWindow = nil;
            }
        }
            break;
        case ZQLogToolManagerLevelText://在控制台显示log及在本地写入log
        {
            [userDefault setInteger:ZQLogToolManagerLevelText forKey:ZQ_LogLevel_UserDefalut];
            [userDefault synchronize];
            
            [self.floatWindow showWindow];
        }
            break;
            
        default:
            break;
    }
}


#pragma mark - getter

- (ZQFloatWindow *)floatWindow
{
    if (_floatWindow == nil)
    {
        //log初始化
        [ZQLog logIntial];
        //        [ZQLog setLogLevel:LOGLEVELD];
        
        //floatWindow 初始化
        _floatWindow = [[ZQFloatWindow alloc] initWithFrame:CGRectMake(0, 200, 50, 50) mainBtnName:@"APP日志" titles:@[@"查看"/*,@"Xcode",@"关闭"*/]];
        
        __weak typeof(self) weakSelf = self;
        
        //点击事件处理
        _floatWindow.clickBlocks = ^(NSInteger i){
            
            switch (i)
            {
                case 0:
                {
                    //显示沙盒本地log
                    [weakSelf displayLocalLog];
                }
                    break;
                case 1:
                {
                    //Xcode显示log
                    weakSelf.logLevel = ZQLogToolManagerLevelLog;
                }
                    break;
                case 2:
                {
                    //隐藏log
                    weakSelf.logLevel = ZQLogToolManagerLevelNone;
                }
                    break;
                default:
                    break;
            }
            
        };
    }
    return _floatWindow;
}

//显示沙盒本地log
- (void)displayLocalLog
{
    if (![ZQLog getLogFilePath])
    {
        return;
    }
    
    //由文件路径初始化UIDocumentInteractionController
    UIDocumentInteractionController *documentInteractionController  = [UIDocumentInteractionController interactionControllerWithURL:[NSURL fileURLWithPath:[ZQLog getLogFilePath]]];
    self.documentInteractionController  = documentInteractionController ;
    documentInteractionController.delegate = self;
    
//    //显示分享文档界面
//    [documentInteractionController  presentOptionsMenuFromRect:[UIScreen mainScreen].bounds inView:[UIApplication sharedApplication].keyWindow.rootViewController.view animated:YES];
    
    //直接显示预览界面
    [documentInteractionController  presentPreviewAnimated:YES];
}

#pragma mark - UIDocumentInteractionControllerDelegate

//在哪个控制器显示预览界面
-(UIViewController *)documentInteractionControllerViewControllerForPreview:(UIDocumentInteractionController *)controller
{
    return self.rootViewController;
}

@end
